﻿using EnetaMvc.ApplicationServices;
using EnetaMvc.ApplicationServices.Dto;
using EnetaMvc.Infrastructure;
using StructureMap;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace EnetaMvc.Web.Extensions
{
    public static class EnetaViewExtensions
    {
        public static UserDto GetCurrentUser(this HtmlHelper helper)
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated)
                return null;

            var userService = ObjectFactory.GetInstance<IUserService>();
            return userService.GetUserByToken(HttpContext.Current.User.Identity.Name);
            
        }
        public static string GetProfileUrl(this HtmlHelper helper, long userId, int sideLength = 32)
        {
            var fileService = ObjectFactory.GetInstance<IFileService>();
            return fileService.GetProfileImageUrl(userId, sideLength);
        }
        public static MvcHtmlString ShowErrorMessage(this HtmlHelper helper, string error)
        {
            return ShowEnetaMessage(error, true);
        }

        public static MvcHtmlString ShowSuccessMessage(this HtmlHelper helper, string message)
        {
            return ShowEnetaMessage(message, false);
        }

        private static MvcHtmlString ShowEnetaMessage(string message, bool isError)
        {
            if (string.IsNullOrWhiteSpace(message))
                return new MvcHtmlString(string.Empty);

            var builder = new StringBuilder();
            builder.AppendLine("<div class=\"slide-box ");
            builder.AppendLine(isError ? "warning" : "success");
            builder.AppendLine("\">");

            builder.AppendLine("    <div class=\"slide-box-center\">");
            builder.AppendLine("        <div class=\"slide-box-bottom\"> <a class=\"close\" href=\"#\">sulge</a>");
            builder.AppendLine("            <div>");
            builder.AppendLine("                <p>");
            builder.AppendLine(message);
            builder.AppendLine("                </p>");
            builder.AppendLine("            </div>");
            builder.AppendLine("        </div>");
            builder.AppendLine("    </div>");
            builder.AppendLine("</div>");

            return new MvcHtmlString(builder.ToString());
        }

        public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> items, string className="")
        {
            var output = new StringBuilder();

            var i = 0;
            foreach (var item in items)
            {
                output.Append(@"<input type=""checkbox"" name=""");
                //output.Append(name + "[" + i + "].Id\"");
                output.Append(name + "\"");
                output.Append(" id=\"" + name + "_" + i + "\"");
                output.Append(" value=\"");
                output.Append(item.Value);
                output.Append("\"");

                if (item.Selected)
                    output.Append(@" checked=""chekced""");

                if (!string.IsNullOrEmpty(className))
                {
                    output.Append(" class=\"");
                    output.Append(className);
                    output.Append("\"");
                }

                output.Append(" />");

                output.Append("<label for=\"");
                output.Append(name + "_" + i);
                output.Append("\">");
                output.Append(item.Text);
                output.Append("</label>");

                i++;
            }

            return new MvcHtmlString(output.ToString());
        }

        public static MvcHtmlString GetHtmlAreaByName(this HtmlHelper helper, string name)
        {
            var contentService = ObjectFactory.GetInstance<IContentService>();
            var htmlArea = contentService.GetHtmlAreaByName(name);

            if (htmlArea == null || !htmlArea.Visible || string.IsNullOrEmpty(htmlArea.Content))
                return null;

            var builder = new StringBuilder();
            builder.AppendLine("<div class=\"slide-box\">");
            builder.AppendLine("    <div class=\"slide-box-center\">");
            builder.Append    ("        <div class=\"slide-box-bottom\"> <a class=\"close\" href=\"#\">");
            builder.Append(Resources.EnetaMvc.Close.ToLower());
            builder.AppendLine("</a>");
            builder.AppendLine("            <div>");
            builder.Append    ("                <p>");
            builder.Append    (htmlArea.Content);
            builder.AppendLine("</p>");
            builder.AppendLine("            </div>");
            builder.AppendLine("        </div>");
            builder.AppendLine("    </div>");
            builder.AppendLine("</div>");

            return new MvcHtmlString(builder.ToString());
        }

        public static MvcHtmlString Tooltip(this HtmlHelper helper, string name, string title)
        {
            var context = HttpContext.Current;

            var builder = new StringBuilder();
            builder.Append("<a href=\"");
            builder.Append("/tooltip/" + name);
            builder.Append("?width=216\" name=\"");
            builder.Append(title);
            builder.Append("\" class=\"jTip\" id=\"help1\">?</a>");

            return new MvcHtmlString(builder.ToString());
        }

        public static MvcHtmlString DisplayDateRange(this HtmlHelper helper, DateTime from, DateTime to)
        {
            var buffer = new StringBuilder(100);

            buffer.Append(@"<li class=""event-date"">");

            buffer.Append(from.ToShortDateString());
            if (from.Date != to.Date)
            {
                buffer.Append(" - ");
                buffer.Append(to.ToShortDateString());
            }

            buffer.Append(@"</li><li class=""time"">");
            buffer.Append(from.ToShortTimeString());
            buffer.Append(" - ");
            buffer.Append(to.ToShortTimeString());

            buffer.Append("</li>");

            return new MvcHtmlString(buffer.ToString());
        }

        public static MvcHtmlString GetPager<T>(this HtmlHelper helper, PagedResult<T> result, string urlPrefix, string cssClass = "small-menu")
        {
            if (result == null || result.PageCount <= 1)
                return new MvcHtmlString("");

            var buffer = new StringBuilder(400);

            buffer.Append("<ul");
            if (!string.IsNullOrEmpty(cssClass))
            {
                buffer.AppendFormat(@" class=""{0}""", cssClass);
            }
            buffer.Append(">");

            if (result.CurrentPage > 1)
            {
                buffer.Append(@"<li class=""first-link"">");
                if (result.CurrentPage == 2)
                {
                    buffer.Append(@"<a href=""");
                    buffer.Append(urlPrefix);
                    buffer.Append(@""">");
                }
                else
                {
                    buffer.Append(@"<a href=""");
                    buffer.Append(urlPrefix);
                    buffer.Append("page/");
                    buffer.Append(result.CurrentPage - 1);
                    buffer.Append(@""">");
                }
                buffer.Append(Resources.EnetaMvc.PreviousOnes);
                buffer.Append("</a></li>");
            }
            if (result.CurrentPage < result.PageCount)
            {
                buffer.AppendFormat(@"<li class=""last-link"">");
                buffer.Append(@"<a href=""");
                buffer.Append(urlPrefix);
                buffer.Append("page/");
                buffer.Append(result.CurrentPage + 1);
                buffer.Append(@""">");
                buffer.Append(Resources.EnetaMvc.NextOnes);
                buffer.Append("</a></li>");
            }

            buffer.Append("</ul>");

            return new MvcHtmlString(buffer.ToString());
        }

        public static MvcHtmlString TagsForList(this HtmlHelper helper, TagDto[] tags, bool showTagsLabel)
        {
            var builder = new StringBuilder();
            builder.AppendLine(@"<ul class=""tags"">");

            if (showTagsLabel)
            {
                builder.AppendFormat("<li><strong>{0}:&nbsp;</strong></li>", Resources.EnetaMvc.Tags);
            }

            for (var i = 0; i < tags.Length; i++)
            {
                builder.AppendFormat("<li>{0}", tags[i].Name);

                if (i < tags.Length - 1)
                    builder.Append(",");

                builder.AppendLine("</li>");
            }

            builder.AppendLine("</ul>");

            return new MvcHtmlString(builder.ToString());
        }

        public static IEnumerable<BlogDto> GetCurrentUserBlogs(this HtmlHelper helper)
        {
            IList<BlogDto> blogs = new List<BlogDto>();

            var userName = HttpContext.Current.User.Identity.Name;
            if (string.IsNullOrEmpty(userName))
                return blogs;

            var rep = ObjectFactory.GetInstance<INewsService>();
            blogs = rep.ListBlogsForUser(userName);

            return blogs;
        }
    }
}